package com.besall.allbase.view.activity.chipstoollevel4.protocol.Settings;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.lib.devices.sgsettingentity.setting.PanelModel;
import bb.lanxing.util.ui.GsonUtils;
import bb.lanxing.utils.PanelModelToJsonUtil;

public class DevicePanelPageActivity extends BaseActivity {
    private static final int REQUEST_CODE_PANEL = 2333;

    private RecyclerView mRecyclerView;
    private DevicePanelPageAdapter mAdapter = null;

    private PanelModel PANEL_MODEL;
    private int panelId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_profiles);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextView title = findViewById(R.id.title);
        title.setText(getIntent().getStringExtra("title"));

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        List<String> list = new ArrayList<>();
        list.add(getString(R.string.device_profiles_setting_detail_panel_list0));
        list.add(getString(R.string.device_profiles_setting_detail_panel_list1));
        list.add(getString(R.string.device_profiles_setting_detail_panel_list2));
        list.add(getString(R.string.device_profiles_setting_detail_panel_list3));
        list.add(getString(R.string.device_profiles_setting_detail_panel_list4));
        list.add(getString(R.string.device_profiles_setting_detail_panel_list5));
        list.add(getString(R.string.device_profiles_setting_detail_panel_list6));
        list.add(getString(R.string.device_profiles_setting_detail_panel_list7));
        PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(getIntent().getStringExtra("PANEL_MODEL"), PanelModel.class);
        int sportMode = getIntent().getIntExtra("sportMode", 0);
        int pageIndex = getIntent().getIntExtra("pageIndex", 0);
        if (PANEL_MODEL != null) {
            panelId = PANEL_MODEL.getProfilesList().get(sportMode).getDisplayData().get(pageIndex).getPanelId();
        }
        mAdapter = new DevicePanelPageAdapter(list, panelId);
        mAdapter.setOnItemClickListener(position -> {
            Intent intent = new Intent(this, DevicePanelSettingActivity.class);
            intent.putExtra("sportMode", sportMode);
            intent.putExtra("pageIndex", pageIndex);
            intent.putExtra("panelId", position + 1);
            intent.putExtra("panelIdOld", panelId);
            intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
            startActivityForResult(intent, REQUEST_CODE_PANEL);
        });
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("PANEL_MODEL", PanelModelToJsonUtil.toJson(PANEL_MODEL));
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_PANEL) {
            PANEL_MODEL = GsonUtils.Companion.getInstance().fromJson(data.getStringExtra("PANEL_MODEL"), PanelModel.class);
            panelId = data.getIntExtra("panelId", -1);
            mAdapter.updateData(panelId);
        }
    }
}